package com.zsl.swing.redis.desktop.window.panel;

import com.zsl.swing.redis.desktop.model.DbCountConfigEntity;
import com.zsl.swing.redis.desktop.model.NodeEntity;
import com.zsl.swing.redis.desktop.type.BtnOptEnum;
import com.zsl.swing.redis.desktop.type.NodeTypeEnum;
import com.zsl.swing.redis.desktop.window.ZslRedisDesktopMainWindow;
import com.zsl.swing.redis.desktop.window.ext.ZslMarkDownTextPane;
import com.zsl.swing.redis.desktop.window.menu.ZslMenuItem;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class ZslShowPanel extends JTabbedPane {

    private static final String TAB_TITLE_FORMAT = "%s[%s-%s]";

    private static final String INDEX_MD_NAME = "instructions.md";

    public ZslShowPanel(){
        super();

        this.addMouseListener(new TabbedPaneMouseAction(this));

        this.setTabPlacement(JTabbedPane.TOP);
        this.addTab("首页", new JScrollPane(new ZslMarkDownTextPane(INDEX_MD_NAME), JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));

    }

    public void addNewTabbedPane(NodeEntity entity){
        String serverName = entity.getParent().getShowName();

        DbCountConfigEntity dbCountConfig = ZslRedisDesktopMainWindow.getDbCountConfig(entity);
        String tabTitle = String.format(TAB_TITLE_FORMAT, serverName, dbCountConfig.getDbShowConfigMap().get(entity.getDbIndex()), entity.getDbIndex());

        ZslDbOptPanel optPanel = ZslDbOptPanel.getInstance(entity);
        this.addTab(tabTitle, NodeTypeEnum.DB.getIcon(), optPanel);

        this.setSelectedComponent(optPanel);
    }


    private static class TabbedPaneMouseAction extends MouseAdapter {

        private ZslShowPanel zslShowPanel;

        public TabbedPaneMouseAction(ZslShowPanel zslShowPanel){
            this.zslShowPanel = zslShowPanel;
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            int tabCount = this.zslShowPanel.getTabCount();

            int index = -1;
            for(int i=0; i< tabCount; i++){
                Rectangle boundsAt = this.zslShowPanel.getBoundsAt(i);
                if(boundsAt.contains(e.getX(),e.getY())){
                    index = i;
                    break;
                }
            }

            if(index != -1){
                int button = e.getButton();
                if(button == MouseEvent.BUTTON3){
                    //右键点击
                    this.doForMouseRightClick(index,e);
                }else
                if(button == MouseEvent.BUTTON1 && e.getClickCount() > 1){
                    //左键双击或者多击
                    this.doForMouseLeftNumsClick(index);
                }
            }
        }

        private void doForMouseLeftNumsClick(int index) {
            this.zslShowPanel.removeTabAt(index);
        }

        private void doForMouseRightClick(int index, MouseEvent e) {
            ZslMenuItem c1 = ZslMenuItem.getInstance(BtnOptEnum.CLOSE.getText(), new TabbedPaneBtnCloseAction(index,this.zslShowPanel));
            ZslMenuItem c2 = ZslMenuItem.getInstance(BtnOptEnum.CLOSE_OTHER.getText(),  new TabbedPaneBtnCloseAction(index,this.zslShowPanel));
            ZslMenuItem c3 = ZslMenuItem.getInstance(BtnOptEnum.CLOSE_ALL.getText(), new TabbedPaneBtnCloseAction(index,this.zslShowPanel));

            JPopupMenu menu = new JPopupMenu();
            menu.add(c1);
            menu.add(c2);
            menu.add(c3);

            menu.show(this.zslShowPanel,e.getX(),e.getY());
        }


    }

    private static class TabbedPaneBtnCloseAction implements ActionListener{

        private int index;

        private ZslShowPanel zslShowPanel;

        public TabbedPaneBtnCloseAction(int index, ZslShowPanel zslShowPanel){
            this.index = index;
            this.zslShowPanel = zslShowPanel;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            String command = e.getActionCommand();
            if(BtnOptEnum.CLOSE.getText().equals(command)){
                this.zslShowPanel.removeTabAt(this.index);
            }else
            if(BtnOptEnum.CLOSE_ALL.getText().equals(command)){
                this.zslShowPanel.removeAll();
            }else
            if(BtnOptEnum.CLOSE_OTHER.getText().equals(command)){
                int tabCount = this.zslShowPanel.getTabCount();
                while(--tabCount >= 0){
                    if(tabCount != index){
                        this.zslShowPanel.removeTabAt(tabCount);
                    }
                }
            }

        }
    }
}
